import { readdirSync } from 'node:fs'
import { join } from 'node:path/posix'

export function findFilesByName({ name, cwd }: { name: string; cwd: string }): string[] {
  const results: string[] = []

  function search(dir: string) {
    const entries = readdirSync(dir, { withFileTypes: true })
    for (const entry of entries) {
      const fullPath = join(dir, entry.name)
      if (entry.isDirectory()) {
        search(fullPath)
      } else if (entry.isFile() && entry.name === name) {
        results.push(fullPath)
      }
    }
  }

  search(cwd)
  return results
}
